import { Component, Injector, ViewEncapsulation } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    BatchTaskServiceProxy,
    BatchDto,
    NewSideTaskInput,
    DepartmentServiceProxy,
    TaskDto,
    NameValueDtoOfInt32,
    PrintServiceProxy,
    ComboboxItemDto,
    PrintCommonDto,
    PrintInput,
    InputSideTaskDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';

@Component({
    selector: 'rw23',
    templateUrl: './rw23.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class RW23ModalComponent extends AppComponentBase {
    saving = false;

    pipe: PipelineListDto = new PipelineListDto();
    batch = new BatchDto();
    teamGroups: ComboboxItemDto[] = [];
    teamGroupName: ComboboxItemDto;
    departments = new Array<NameValueDtoOfInt32>();
    departmentValue: number;
    printInput = new PrintCommonDto();
    receptionTime = new Date();
    sideProperty = '';
    oldprint = '';
    inputSideTaskDto=new InputSideTaskDto();
    constructor(
        injector: Injector,
        private _departmentServiceProxy: DepartmentServiceProxy,
        private _taskServiceProxy: BatchTaskServiceProxy,
        private _printServiceProxy: PrintServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;

        this._departmentServiceProxy.getWorkshops().subscribe(result => {
            this.departments = result.items;
            //this.departmentValue = result.items[0].value;
        });

        this._taskServiceProxy.getBatch(pipe.taskId).subscribe(result => {
            this.batch = result;

            console.log(result);
        });
        this._taskServiceProxy
        .getDefaultDeliveryTarget(pipe.taskId)
        .subscribe(result => {
            this.departmentValue = result;
        });
        this._printServiceProxy.getPrintDrow().subscribe(result => {
            // this.teamGroupName = result.groupName;
            this.teamGroups = result;
            this._printServiceProxy.getUserPrint().subscribe(result1 => {
                if(result1!=null){
                    this.oldprint = result1.value;
                }
                this.teamGroupName = result1;
            });
        });
    }

    save(): void {
        this.saving = true;

        let self = this;

        let input = new NewSideTaskInput();
        input.id = this.pipe.taskId;
        input.drawingCode = this.pipe.drawingCode;
        input.drawingName = this.pipe.taskName;
        input.quantity = this.pipe.amountPlanned;
        input.producerId = this.departmentValue;
        input.sideProperty = this.sideProperty;
        input.receptionTime=moment(this.receptionTime) ;
        this._taskServiceProxy
            .newSideTask(input)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(result => {
                this.notify.success('生成定制单成功');
                this._taskServiceProxy
                    .getBatch(self.pipe.taskId)
                    .subscribe(result => {
                        this.batch = result;
                    });
            });
    }

    print(data: TaskDto): void {
        console.log(data);
        this.primengTableHelper.showLoadingIndicator();
    if ( this.oldprint != this.teamGroupName.value){
        this._printServiceProxy.createOrUpdateUserPrint(this.teamGroupName.value).subscribe(result=>{})
    }

    this.inputSideTaskDto.id=data.id;
    this.inputSideTaskDto.receptionTime=moment(this.receptionTime) ;
    this.inputSideTaskDto.remark=data.remark;
    this.inputSideTaskDto.sideProperty=data.sideProperty;
    this._printServiceProxy.updateSideTask(this.inputSideTaskDto).subscribe(result=>{})

    this.printInput.printInput=new PrintInput();
        this.printInput.printSet = this.teamGroupName.value;
        this.printInput.type = 'DingZhiDan';
        this.printInput.printInput.dingZhi = data.originalProducerId + '车间';
        this.printInput.printInput.zhiXing = data.producerId + '车间';
        this.printInput.printInput.dingZhiTime = data.creationTime;
        this.printInput.printInput.taskCode = data.taskCode;
        this.printInput.printInput.dingZhiXingZhi = data.sideProperty;
        this.printInput.printInput.songHuiTime =moment( this.receptionTime);
        this.printInput.printInput.name = data.drawingName;
        this.printInput.printInput.drawCode = data.drawingCode;
        this.printInput.printInput.remark = data.remark;
        this.printInput.printInput.num = data.totalQuantity;
        this._printServiceProxy.print(this.printInput).subscribe(result=>{
            this.notify.success('生成定制单成功');
            this.primengTableHelper.hideLoadingIndicator();
        })
    }
}
